package cn.ges.rabbitmq

import org.springframework.amqp.core.AmqpTemplate
import org.springframework.amqp.core.Message
import org.springframework.amqp.core.MessagePostProcessor

/**
 * @author gespent@163.com
 * @date 2020/04/14 14:20
 */
class RabbitSend() {


    companion object {
        fun init(rabbitTemplate: AmqpTemplate) {
            Companion.rabbitTemplate = rabbitTemplate
        }

        private var rabbitTemplate: AmqpTemplate? = null

        fun send(queue: String?, msg: String?) {
            rabbitTemplate!!.convertAndSend(queue, msg)
        }

        fun sendExchange(exchange: String?, msg: String?) {
            rabbitTemplate!!.convertAndSend(exchange, "", msg)
        }

        fun send(queue: String?, msg: String?, times: Long) {
            val processor = MessagePostProcessor { message: Message ->
                message.messageProperties.expiration = times.toString()
                message
            }
            rabbitTemplate!!.convertAndSend(queue, msg as Any?, processor)
        }

        fun sendTtlMsg(msg: String?, ttlQueueName: String?, millisecond: Long) {
            val processor = MessagePostProcessor { message: Message ->
                message.messageProperties.expiration = millisecond.toString()
                message
            }
            ttlQueueName?.let {
                rabbitTemplate!!.convertAndSend("TTL_AE", msg as Any?, processor)
                return
            }
            rabbitTemplate!!.convertAndSend(ttlQueueName, msg as Any?, processor)
        }
    }

}